#include "BlockQueue.hpp"
#include "APILock.hpp"
#include "RingQueue.hpp"
#define CON 3 // 消费者线程数量
#define PRO 3 // 生产者线程数量
// template<class T>
BlockQueue<int> bq; // 阻塞队列
int data = 0;
pthread_mutex_t mutex;
RingQueue<int> rq;
void FunPro(std::string name)
{

    while (true)
    {
         bq.push(data);
         std::cout << name << data << std::endl;
        //rq.push(data);
        data++;
        sleep(1);
    }
}
void FunCon(std::string name)
{
    int out = 0;//
    while (true)
    {
         bq.pop(&out);
        // APIlock(&mutex);
        //rq.pop(&out);
        pthread_mutex_lock(&mutex);
        std::cout << name << out << std::endl;
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}

int main()
{
    pthread_mutex_init(&mutex, nullptr);
    std::vector<std::thread> th;
    for (int i = 0; i < PRO; i++)
        th.push_back(std::thread(FunPro, "pro_" + std::to_string(i) + " "));
    for (int i = 0; i < CON; i++)
        th.push_back(std::thread(FunCon, "con_" + std::to_string(i) + " "));
    for (int i = 0; i < CON + PRO; i++)
        th[i].join();
    return 0;
}